package com.ex.orage2app;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import pl.com.salsoft.sqlitestudioremote.SQLiteStudioService;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{
private EditText usernameText,paswdEdit;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
//增加的sql
        SQLiteStudioService.instance().start(this);
        usernameText=findViewById(R.id.username);
        paswdEdit=findViewById(R.id.password);
        Button register=findViewById(R.id.register);
        Button login=findViewById(R.id.login);
        register.setOnClickListener(this);
        login.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.register) {
            Intent intent1 = new Intent(MainActivity.this, RegisterActivity.class);
            startActivity(intent1);
        } else if (id == R.id.login) {
            if (validateLogin()) {
                Intent intent2 = new Intent(MainActivity.this, IndexActivity.class);
                Bundle bundle = new Bundle();
                OrangeDatabase orangeDatabase = new OrangeDatabase(MainActivity.this);
                bundle.putString("username", usernameText.getText().toString());
                bundle = orangeDatabase.queryUserInfo(
                        orangeDatabase.getReadableDatabase(), bundle);
                intent2.putExtras(bundle);
                startActivity(intent2);
            } else {
                Toast.makeText(MainActivity.this, "账号或者密码错误", Toast.LENGTH_LONG).show();
            }
        }

    }
    private boolean validateLogin(){
        String username=usernameText.getText().toString();
        String password=paswdEdit.getText().toString();
        OrangeDatabase orangeDatabase=new OrangeDatabase(MainActivity.this);
        SQLiteDatabase sqLiteDatabase=orangeDatabase.getReadableDatabase();
        Cursor cursor = sqLiteDatabase.rawQuery("select * from orange_user where username=? and password=?", new String[]{username, password});
        if(cursor.getCount()>0){
            return true;
        }
        return false;
    }
}